package com.kancy.xml.utils;

import javax.xml.bind.*;
import java.io.*;
import java.net.URL;

public class XmlUtils {

    /**
     * xml转成Object
     * @param fileObject : File、URL、Reader、InputStream
     * @param clazz : 转换的类型
     * @return
     */
    public static <T> T xmlToObject(Object fileObject , Class<T> clazz){
        Object deserializationObject = null;
        try {
            if(fileObject == null) return null;
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            if(fileObject instanceof File){
                deserializationObject = unmarshaller.unmarshal((File) fileObject);
            }else if(fileObject instanceof String){
                deserializationObject = unmarshaller.unmarshal(new StringReader(String.valueOf(fileObject)));
            }else if(fileObject instanceof URL){
                deserializationObject = unmarshaller.unmarshal((URL) fileObject);
            }else if(fileObject instanceof Reader){
                deserializationObject = unmarshaller.unmarshal((Reader) fileObject);
            }else if(fileObject instanceof InputStream){
                deserializationObject = unmarshaller.unmarshal((InputStream) fileObject);
            }else {
                throw new IllegalArgumentException("Unsupported " + fileObject.getClass());
            }
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return (T) deserializationObject;
    }

    public static void objectToxml(Object object , File file){
        try {
            Marshaller marshaller = getMarshaller(object);
            marshaller.marshal(object, file);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    public static void objectToxml(Object object , Writer writer , boolean isClose){
        try {
            Marshaller marshaller = getMarshaller(object);
            marshaller.marshal(object, writer);
        } catch (JAXBException e) {
            e.printStackTrace();
        }finally {
            if(isClose){
                if(writer != null){
                    try {
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void objectToxml(Object object , OutputStream outputStream , boolean isClose){
        try {
            Marshaller marshaller = getMarshaller(object);
            marshaller.marshal(object, outputStream);
        } catch (JAXBException e) {
            e.printStackTrace();
        }finally {
            if(isClose && outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static Marshaller getMarshaller(Object object) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING,"utf-8");//编码格式
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);//是否省略xml头信息，默认false
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);//是否格式化生成的xml串,默认false
        return marshaller;
    }

    public static String objectToxml(Object object){
        StringWriter writer = new StringWriter();
        objectToxml(object, writer,false);
        return writer.toString();
    }
}
